@include('admin.public.head')
@include('admin.public.laydate')

<div class="ibox-title">
    <h5>輪播圖添加</h5>
</div>

<div class="ibox-content">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="col-sm-3 control-label">門店分類：</label>
            <div class="col-sm-9">
                <select class="form-control" name="store_id" id="store_id">
                    <option value="">請選擇門店</option>
                    @foreach($select_store as $k=>$v)
                        <option value="{{$k}}"
                                @if(isset($result->store_id))@if($result->store_id == $k) selected="selected" @endif @endif>{{$v}}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">優惠卷標題：</label>
            <div class="col-sm-9">
                <input type="text" name="coupon_title" class="form-control" placeholder="優惠卷標題"
                       value="{{$result->coupon_title??''}}">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">優惠卷價值：</label>
            <div class="col-sm-9">
                <input type="text" name="coupon_value" class="form-control" placeholder="優惠卷標題"
                       value="{{$result->coupon_value??''}}">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">備注：</label>
            <div class="col-sm-9">
               <textarea class="form-control" name="remarks" rows="3" cols="40"
                         style="height: 150px">{!! $result->remarks??'' !!}</textarea>
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否驗證過期：</label>
            <div class="col-sm-9">
                <label class="radio-inline">
                    <input type="radio" @if(isset($result->is_expiration)) @if($result->is_expiration == 1) checked
                           @endif @endif value="1" name="is_expiration">是</label>
                <label class="radio-inline">
                    <input type="radio" @if(isset($result->is_expiration)) @if($result->is_expiration == 2) checked
                           @endif @endif value="2" name="is_expiration">否</label>
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否驗證過期：</label>
            <div class="col-sm-9">
                @php
                    $expiration_time = $result->expiration_time??"";
                    $expiration_time_result = explode(" ", $expiration_time);
                @endphp
                <input type="text" value="{{$expiration_time_result[0]??''}}" id="start_time" class="form-control"
                       name="expiration_time" autocomplete="off">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">兌換罵：</label>
            <div class="col-sm-9">
                <input type="text" value="{{$result->coupon_code??redemption_code()}}" name="coupon_code"
                       class="form-control" disabled="disabled">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否放開：</label>
            <div class="col-sm-9">
                <label class="radio-inline">
                    <input type="radio" @if(isset($result->is_opend)) @if($result->is_opend == 1) checked
                           @endif @endif value="1" name="is_opend">是</label>
                <label class="radio-inline">
                    <input type="radio" @if(isset($result->is_opend)) @if($result->is_opend == 2) checked
                           @endif @endif value="2" name="is_opend">否</label>
            </div>
        </div>
        <div class="hr-line-dashed"></div>
        <div class="form-group">
            <label class="col-sm-3 control-label">最大领取数：</label>
            <div class="col-sm-9">
                <input type="number" name="max_number" class="form-control" placeholder="最大领取数"
                       value="{{$result->max_number??''}}">
            </div>
        </div>
        <div class="hr-line-dashed"></div>


        <input type="hidden" name="id" value="{{$id??0}}">

        <div class="form-group">
            <div class="col-sm-4 col-sm-offset-3">
                <button class="btn btn-primary do" type="submit">操作</button>
                <a href="/coupon" class="btn btn-primary">返回優惠卷</a>
            </div>
        </div>
    </form>
</div>

<script>
    $('.do').click(function () {
        var form = new FormData();

        var id = $('input[name="id"]').val();
        var coupon_title = $('input[name="coupon_title"]').val();
        var coupon_value = $('input[name="coupon_value"]').val();
        var expiration_time = $('input[name="expiration_time"]').val();
        var max_number = $('input[name="max_number"]').val();
        var coupon_code = $('input[name="coupon_code"]').val();
        var is_expiration = $('input[name="is_expiration"]:checked').val();
        var is_opend = $('input[name="is_opend"]:checked').val();
        var textarea = $('textarea[name="remarks"]').val();
        var store_id = $('select[name="store_id"] option:selected').val();

        if (typeof (is_expiration) == 'undefined') is_expiration = '';
        if (typeof (is_opend) == 'undefined') is_opend = '';

        form.append("id", id);
        form.append("coupon_title", coupon_title);
        form.append("coupon_value", coupon_value);
        form.append("expiration_time", expiration_time);
        form.append("max_number", max_number);
        form.append("coupon_code", coupon_code);
        form.append("is_expiration", is_expiration);
        form.append("is_opend", is_opend);
        form.append("remarks", textarea);
        form.append("store_id", store_id);

        $.ajax({
            type: 'POST',
            url: '/coupon_do',
            dataType: "json",   //返回格式为json
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {

                if (data.status == '1') {
                    var swal_json = {
                        title: data.message,
                        type: "success"
                    }

                    swal(swal_json, function (isConfirm) {
                        window.location.reload();
                    });

                } else {
                    var swal_json = {
                        title: data.message,
                        type: "error"
                    }

                    swal(swal_json, function (isConfirm) {

                    });
                }
            }
        });

        return false;
    });
</script>
